import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

public class Hall {
    private static HashMap<String, Hall> hallMap = new HashMap<>();

    private String hallName;//放映厅名称
    private int rows;//行数
    private int cols;//列数

    static {
        // 初始化5个放映厅
        hallMap.put("黄金厅", new Hall("黄金厅", 8, 10));
        hallMap.put("白银厅", new Hall("白银厅", 10, 12));
        hallMap.put("翡翠厅", new Hall("翡翠厅", 7, 8));
        hallMap.put("钻石厅", new Hall("钻石厅", 7, 8));
        hallMap.put("宝石厅", new Hall("宝石厅", 8, 9));
    }

    public Hall(String name, int rows, int cols) {
        this.hallName = name;
        this.rows = rows;
        this.cols = cols;
    }

    // 获取放映厅信息
    public static Hall getHall(String hallName) {
        return hallMap.get(hallName);
    }

    // 获取总座位数
    public int getTotalSeats() {
        return rows * cols;
    }

    // 获取放映厅名称
    public String getHallName() {
        return hallName;
    }

    // 获取排数
    public int getRows() {
        return rows;
    }

    // 获取每排座位数
    public int getCols() {
        return cols;
    }

    public static HashMap<String, Hall> getHallMap() {
        return hallMap;
    }

    public static void setHallMap(HashMap<String, Hall> hallMap) {
        Hall.hallMap = hallMap;
    }

    public void setHallName(String hallName) {
        this.hallName = hallName;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void setCols(int cols) {
        this.cols = cols;
    }


    // 显示所有放映厅信息
    public static void displayAllHalls() {
        System.out.println("\n=== 万东电影院放映厅信息 ===");
        for (Hall hall : hallMap.values()) {
            System.out.println(hall.hallName + ": " + hall.rows + "排 × " + hall.cols + "座，总座位数: " + hall.getTotalSeats());
        }
    }

    //选择影厅//使得返回有效的hall
    public static Hall selectHall() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("可用影厅列表：");

        // 显示所有影厅
        int i = 0;
        String[] hallNames = new String[hallMap.size()];
        for (String name : hallMap.keySet()) {
            System.out.println(i + ". " + name);
            hallNames[i] = name;
            i++;
        }

        System.out.print("请选择影厅编号：");
        int choice = scanner.nextInt();
        scanner.nextLine();

        // 验证选择是否有效
        if (choice >= 0 && choice < hallNames.length) {
            return hallMap.get(hallNames[choice]);
        } else {
            System.out.println("无效的选择，将使用默认影厅");
            return hallMap.get("黄金厅"); // 返回默认影厅
        }
    }


    //辅助方法：根据电影名称获取电影时长
    public static int getMovieDuration(String movieName) {
        for (Movies movie : Movies.getMovieList()) {
            if (movie.movieName.equals(movieName)) {
                try {
                    // 直接解析为整数分钟数
                    return Integer.parseInt(movie.movieLength.trim());
                } catch (NumberFormatException e) {
                    System.out.println("电影《" + movieName + "》时长格式错误：" + movie.movieLength);
                    return -1;
                }
            }
        }
        System.out.println("未找到电影《" + movieName + "》");
        return -1;
    }

    public static Hall getHallByName(String hallName) {
        for (Hall hall : hallMap.values()) {
            if (hall.getHallName().equals(hallName)) {
                return hall;
            }
        }
        return null;
    }

    public static LinkedList<Hall> getAllHalls() {
        return new LinkedList<>(hallMap.values());
    }
}
